package com.appkubes.mall.restful.application.pay.strategy.jifen;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.restful.application.order.order.service.OrderService;
import com.appkubes.mall.restful.application.pay.constants.PayConstants;
import com.appkubes.mall.restful.application.pay.entity.dto.PayOrderDTO;
import com.appkubes.mall.restful.application.pay.service.PayService;
import com.appkubes.mall.restful.application.system.config.service.impl.ConfigServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Author: Rongjie
 * @Date: 2020/2/23
 * @Description:
 */
@Service
public class PayPointServiceImpl implements PayService {

    @Autowired
    ConfigServiceImpl configServiceImpl;

    @Resource
    OrderService orderService;

    @Override
    public PayConstants.PaymentType getPaymentType() {
        return PayConstants.PaymentType.JIFEN;
    }

    @Override
    public PayOrderDTO pay(String orderKey) {
        PayOrderDTO payOrderDTO = new PayOrderDTO();
        payOrderDTO.setOrderKey(orderKey);
        payOrderDTO.setPaymentType(PayConstants.PaymentType.JIFEN.name());

        String creditPointPay = configServiceImpl.getByKey("credit_point_pay", "0");
        if (creditPointPay.equals("0")) {
            payOrderDTO.setPayMessage("支付失败，未配置积分支付比例");
            return payOrderDTO;
        }

        MallOrder order = orderService.getById(orderKey);
        //计算积分
        BigDecimal payCredits = order.getAmount().multiply(new BigDecimal(creditPointPay));
        order.setPayCredits(payCredits); //使用积分进行支付
        order.setPayState(1);
        order.setPayTime(DateUtils.getCurrentTimestamp());
        order.setUpdateTime(DateUtils.getCurrentTimestamp());
        orderService.updateById(order);
        payOrderDTO.setStatus(1);
        return payOrderDTO;
    }
}
